// firstPackage/pages/platformCompanyHouse/platformCompanyHouse.js

import { getCompanyEnterHouse } from "../../../service/platform_service"
import { attentionAction } from "../../../service/attention_service"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseList: [

    ],
    companyNumber: null,
    companyName:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      companyNumber: options.companyNumber,
      companyName:options.name
    })
    this.getCompanyEnterHouse();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //查询代理房源列表:
  getCompanyEnterHouse() {
    wx.showLoading();
    getCompanyEnterHouse(this.data.companyNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:列表结果是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          houseList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:getCompanyEnterHouse", err);
    })
  },

  //其它关注
  collectAction(e) {
    console.log("JJLog:e:" + JSON.stringify(e));
    let { data, index, type } = e.detail;
    this.attentionAction(data, type, index);
  },
  //关注操作
  attentionAction(item, type, index) {
    let data = {
      collectType: 1,
      number: item.houseNumber
    }
    wx.showLoading();
    attentionAction(data).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        item.isCollect = !item.isCollect;
        //动态数组:
        if (type == "bottom") {
          let changeItem = 'houseList[' + index + '].isCollect';
          this.setData({
            [changeItem]: item.isCollect,
          })
        }
        wx.showToast({
          title: item.isCollect ? "关注成功" : "取关成功",
          icon: "none"
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:attentionAction", err);
    })
  },





})